#version 460 core
#define GLSLIFY 1

#define M_PI 3.1415926535897932384626433832795

 
layout(points) in;
layout(line_strip, max_vertices=64) out;
//layout(lines, max_vertices=128) out;

#define GLSLIFY 1
#define PROJECT_MATRIX

/*
0 投影矩阵
1 属性参数
2 变换数据
3 颜色数据

4 顶点数据
5 顶点配置参数


6 颜色控制参数

10 纹理数据

*/

layout(std140, set=0, binding=0) uniform U_Matrix {
	mat4 mat_ProjecView;
};

layout(std140, binding = 1) uniform U1_Matrix {
	//边界
	vec2 m_Scale;
	vec2 m_Loc;
	//顶点总数量一半
	int m_GradientNum;
	int vertNum;
};

// 位置  大小 缩放和深度
layout(std430, column_major, binding = 2) readonly buffer U2_Matrix {
	mat3x2 m_LP_SD[];
};

#define GLSLIFY 1
#define VERTEX_IN

struct S_Vertex_in {
	vec2 coord;
	vec2 weight;
	vec2 normal;
};

layout(std430, column_major, binding = 4) readonly buffer U4_Matrix {
	//mat3x2 m_Vertex[];
	S_Vertex_in m_Vertex[];
};

#define GLSLIFY 1
#define SINGLE_COLOR

layout(std430, column_major, binding = 3) readonly buffer U3_Matrix {
	uint InstanceColor[];
};

vec4 f_buildColor(in uint color){
	vec4 TColor;
	TColor.r = float((color & 0xff000000) >> 24) / 255.0;
	TColor.g = float((color & 0x00ff0000) >> 16) / 255.0;
	TColor.b = float((color & 0x0000ff00) >> 8) / 255.0;
	TColor.a = float((color & 0x000000ff)) / 255.0;

	//TColor = vec4(0.5, 0.6, 0.2, 0.8);
	return TColor;
}

/*vec4 f_buildColor(in uint color){
	vec4 TColor;
	TColor.a = float((color & 0xff000000) >> 24) / 255.0;
	TColor.b = float((color & 0x00ff0000) >> 16) / 255.0;
	TColor.g = float((color & 0x0000ff00) >> 8) / 255.0;
	TColor.r = float((color & 0x000000ff)) / 255.0;
	return TColor;
}*/

//图元 顶点偏移和顶点数量 
layout(std430, column_major, binding = 5) readonly buffer U5_Matrix {
	uvec2 m_VertOffset_Num[];
};

in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	uint InstanceIndex;
}vertex_in[];

layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};

void main(void){
	uint instance = vertex_in[0].InstanceIndex;
	mat3x2 mat = m_LP_SD[instance];
	
	vec2 size = mat[1] - mat[0];
	float PN = sign(size.x);
	
	vec4 TColor = f_buildColor(InstanceColor[instance]);
	uvec2 VertOffsetNum = m_VertOffset_Num[instance];

	uint num = VertOffsetNum.y - VertOffsetNum.x;
	for(uint i=0; i<num; ++i){
		vec4 v = vec4(m_Vertex[VertOffsetNum.x + i].coord, 0, 1);
		vec2 Weights = m_Vertex[VertOffsetNum.x + i].weight;
		vec2 normal = m_Vertex[VertOffsetNum.x + i].normal;

		//v.xy = (v.xy * mat[2].x)    +    ((mat[1]-mat[2].x) * Weights.xy);
		vec2 Invert = sign(mat[1]);
		v.xy = (v.xy * (mat[2].x) * Invert)    +    ((mat[1]-mat[2].x*Invert) * Weights.xy);
		
		
		v.xy += mat[0];
		v.xy *= m_Scale;
		v.xy += m_Loc;
		
		gl_Position = mat_ProjecView * v;
		gl_PointSize = mat[2].x;
		gColor = TColor;
		EmitVertex();
	}

	EndPrimitive();
}

 